package com.olympia.myapp.custom;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.olympia.myapp.R;
import com.olympia.myapp.util.AnimationControl;
import com.olympia.myapp.util.ControlListener;
import com.olympia.myapp.util.Reminder;

public class Dialog_VCNV extends Dialog implements OnClickListener {
	Context activity;
	ProcessDialogVCNV process;
	EditText edtAnswer;

	Button btnCancel;
	TextView txtContent;

	Reminder remind = null;
	int maxTime = 15;
	int time = 15;
	boolean isRun = true;
	AnimationControl animatCotrol = null;

	/**
	 * 
	 * @param context
	 */
	public Dialog_VCNV(Context context, String content, int second,
			ProcessDialogVCNV pro) {
		super(context);
		activity = (Activity) context;
		/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		/** Design the dialog in main.xml file */
		setContentView(R.layout.dialog_vcnv);
		animatCotrol = new AnimationControl();
		this.process = pro;

		btnCancel = (Button) findViewById(R.id.dg_vcnv_btnCancel);
		edtAnswer = (EditText) findViewById(R.id.dg_vcnv_edt);
		txtContent = (TextView) findViewById(R.id.dg_vcnv_content);
		btnCancel.setOnClickListener(this);

		setContent(content);

		time = maxTime = second;
		remind = new Reminder(new ControlListener() {
			public void runTask() {
				if (isRun) {
					time--;
					process.click_Run(String.valueOf(time));
				}
			}

			public void stopTask() {
				dismiss();
				isRun = false;
				process.click_Stop(edtAnswer.getText().toString());
			}
		}, maxTime);
		remind.start(1);
		isRun = true;
	}

	public void setContent(String content) {
		if (txtContent != null) {
			txtContent.setText(content);
			animatCotrol.RunAnimations(activity, txtContent, R.anim.slide_right);
			animatCotrol.RunAnimations(activity, btnCancel, R.anim.alpha);
			animatCotrol.RunAnimations(activity, edtAnswer, R.anim.alpha);
		}
	}

	@Override
	public void onClick(View v) {
		if (v == btnCancel) {
			isRun = false;
			process.click_Cancel();
			dismiss();
		}
	}

	public static abstract class ProcessDialogVCNV {
		public abstract void click_Cancel();

		public abstract void click_Stop(String ans);

		public abstract void click_Run(String time);
	}
}
